package com.kidult.android.example;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import com.example.dragableview.R;
import com.kidult.android.common.view.PullToRefreshBase.PullModeEnum;
import com.kidult.android.common.view.PullToRefreshScrollView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class PullToRefreshScrollViewActivity extends Activity {
    private final int INIT_LISTVIEW = 1000;
    private final int REFRESH_LIST_VIEW_AT_TOP = 1001;
    private final int REFRESH_LIST_VIEW_AT_BUTTOM = 1002;

    private final int INIT_LISTVIEW_2 = 2000;
    private final int REFRESH_LIST_VIEW_AT_TOP_2 = 2001;
    private final int REFRESH_LIST_VIEW_AT_BUTTOM_2 = 2002;

    private PullToRefreshScrollView sv;
    private ProgressBar mainProgress;

    private HandlerThread hr = new HandlerThread("handler_thread");

    {
	hr.start();
    }

    private Handler secondaryHandler = new Handler(hr.getLooper()) {
	@Override
	public void handleMessage(android.os.Message msg) {

	    switch (msg.what) {
	    case INIT_LISTVIEW_2:
		new MyThread(mainActivityHandler, INIT_LISTVIEW).start();
		break;
	    case REFRESH_LIST_VIEW_AT_TOP_2:
		consumeOperator();
		new MyThread(mainActivityHandler, REFRESH_LIST_VIEW_AT_TOP).start();
		break;
	    case REFRESH_LIST_VIEW_AT_BUTTOM_2:
		consumeOperator();
		new MyThread(mainActivityHandler, REFRESH_LIST_VIEW_AT_BUTTOM).start();
		break;
	    default:
		break;
	    }
	};
    };

    private Handler mainActivityHandler = new Handler() {

	@Override
	public void handleMessage(android.os.Message msg) {
	    switch (msg.what) {
	    case INIT_LISTVIEW:
		mainProgress.setVisibility(View.GONE);
		addItems();
		break;
	    case REFRESH_LIST_VIEW_AT_TOP:
		Calendar date = Calendar.getInstance();
		sv.onRefreshComplete("最后更新于：" + date.get(Calendar.YEAR) + "年" + date.get(Calendar.MONTH) + "月"
			+ date.get(Calendar.DAY_OF_MONTH) + "日" + date.get(Calendar.HOUR_OF_DAY) + ":"
			+ date.get(Calendar.MINUTE));
		break;
	    case REFRESH_LIST_VIEW_AT_BUTTOM:
		sv.onRefreshComplete();
		break;
	    default:
		break;
	    }
	};
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_pull_to_refresh_scrollview);
	initView();
    }

    private void initView() {
	sv = (PullToRefreshScrollView) findViewById(R.id.activity_main_sv);
	sv.setCurrentMode(PullModeEnum.BOTH);
	mainProgress = (ProgressBar) findViewById(R.id.main_progress);
	new MyThread(secondaryHandler, INIT_LISTVIEW_2).start();
	sv.setOnRefreshHeaderListener(new PullToRefreshScrollView.OnRefreshHeaderListener() {

	    @Override
	    public void onRefreshHeader() {
		new MyThread(secondaryHandler, REFRESH_LIST_VIEW_AT_TOP_2).start();
	    }

	});
	sv.setOnRefreshFooterListener(new PullToRefreshScrollView.OnRefreshFooterListener() {

	    @Override
	    public void onRefreshFooter() {
		new MyThread(secondaryHandler, REFRESH_LIST_VIEW_AT_BUTTOM_2).start();
	    }
	});
	// LayoutInflater inflater = getLayoutInflater();
	// relativeLayout = (RelativeLayout) inflater.inflate(
	// R.layout.drop_down_to_refresh_list_footer, null);
	// footer = (TextView) relativeLayout
	// .findViewById(R.id.drop_down_to_refresh_list_footer_text);
	// footer.setText(R.string.drop_down_to_refresh_list_refresh_view_tips);
	// progress = (ProgressBar) relativeLayout
	// .findViewById(R.id.drop_down_to_refresh_list_footer_progress);
	// relativeLayout.setOnClickListener(new View.OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// footer.setText(R.string.drop_down_to_refresh_list_refreshing_tips);
	// progress.setVisibility(View.VISIBLE);
	// relativeLayout.setClickable(false);
	// new MyThread(secondaryHandler, REFRESH_LIST_VIEW_AT_BUTTOM_2)
	// .start();
	// }
	// });
	// lv.addFooterView(relativeLayout);
    }

    private List<String> getInternetData() {
	consumeOperator();
	// List<String> data = new ArrayList<String>();
	// if (contextData == null) {
	// for (int i = 0; i < 20; i++)
	// data.add(i + " items");
	// Collections.reverse(data);
	// } else {
	// for (int i = contextData.size(); i < (contextData.size() + 10); i++)
	// data.add(i + " New Items");
	// Collections.reverse(data);
	// }
	// return data;
	return null;
    }

    private List<String> loadOldData() {
	consumeOperator();
	// List<String> data = new ArrayList<String>();
	// if (contextData == null) {
	// for (int i = 0; i < 20; i++)
	// data.add(i + " items");
	// Collections.reverse(data);
	// } else {
	// for (int i = contextData.size(); i < (contextData.size() + 10); i++)
	// data.add(i + " Old Items");
	// Collections.reverse(data);
	// }
	// return data;
	return null;
    }

    private void addItems() {
	Button btn1 = new Button(this);
	btn1.setText("btn1");
	Button btn2 = new Button(this);
	btn2.setText("btn2");
	Button btn3 = new Button(this);
	btn3.setText("btn3");
	Button btn4 = new Button(this);
	btn4.setText("btn4");
	Button btn5 = new Button(this);
	btn5.setText("btn5");
	Button btn6 = new Button(this);
	btn6.setText("btn6");
	Button btn7 = new Button(this);
	btn7.setText("btn7");
	Button btn8 = new Button(this);
	btn8.setText("btn8");
	Button btn9 = new Button(this);
	btn9.setText("btn9");
	Button btn10 = new Button(this);
	btn10.setText("btn10");
	Button btn11 = new Button(this);
	btn11.setText("btn11");
	Button btn12 = new Button(this);
	btn12.setText("btn12");
	Button btn13 = new Button(this);
	btn13.setText("btn13");
	Button btn14 = new Button(this);
	btn14.setText("btn14");
	Button btn15 = new Button(this);
	btn15.setText("btn15");
	Button btn16 = new Button(this);
	btn16.setText("btn16");
	Button btn17 = new Button(this);
	btn17.setText("btn17");
	LinearLayout ll = new LinearLayout(this);
	ll.setOrientation(LinearLayout.VERTICAL);
	ll.addView(btn1);
	ll.addView(btn2);
	ll.addView(btn3);
	ll.addView(btn4);
	ll.addView(btn5);
	ll.addView(btn6);
	ll.addView(btn7);
	ll.addView(btn8);
	ll.addView(btn9);
	ll.addView(btn10);
	ll.addView(btn11);
	ll.addView(btn12);
	ll.addView(btn13);
	ll.addView(btn14);
	ll.addView(btn15);
	ll.addView(btn16);
	ll.addView(btn17);
	sv.getmPullableView().addView(ll);
    }

    private void consumeOperator() {
	try {
	    Thread.sleep(1000);
	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
    }

    private void updateListViewDataAtTop(List<String> srcList, List<String> tarList) {
	srcList.addAll(0, tarList);
    }

    private void loadListViewDataAtButtom(List<String> srcList, List<String> tarList) {
	srcList.addAll(srcList.size(), tarList);
    }

    class MyThread extends Thread {
	Handler handler;
	int messageCode;

	public MyThread(Handler handler, final int messageCode) {
	    this.handler = handler;
	    this.messageCode = messageCode;
	}

	@Override
	public void run() {
	    handler.sendEmptyMessage(messageCode);
	}
    }
}
